

package ws_sax;

import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXProcessing extends DefaultHandler {

    static int count = 0;
    
    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Uasge: java SAXProcessing tomcat-users.xml");
            System.exit(1);
        }
        
        
        SAXProcessing saxObject = new SAXProcessing();

        SAXParserFactory spf = SAXParserFactory.newInstance();
            try {
                SAXParser parser = spf.newSAXParser();
                parser.parse(new File(args[0]), saxObject);
            } catch (Exception e){
                e.printStackTrace();
                System.exit(1);
            }
        

    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equals("user")) {
            count++;
        }
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println(count + " users defined on Tomcat Server");
    }

}


